package org.example.book.decorator;

import org.example.book.mediator.AbstractCustomer;
import org.example.book.mediator.Buyer;
import org.example.book.mediator.Medicator;
import org.example.book.mediator.Payer;
import org.example.book.pojo.Products;
import org.example.book.repo.ProductsRepository;
import org.example.book.service.OrderServiceInterface;
import org.example.book.state.pojo.Order;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class OrderServiceDecorator extends AbstractOrderServiceDecorator {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Value("delay.service.time")
    private String delayServiceTime;

    @Autowired
    private ProductsRepository productsRepository;

    @Autowired
    private Medicator medicator;

    @Override
    protected void updateScoreAndSendRedPaper(String productId, int serviceLevel, float price) {
        switch (serviceLevel) {
            case 0:
                // 实时
                int score = Math.round(price) * 100;
                System.out.println("正常处理，score = " + score);
                Products byProductId = productsRepository.findByProductId(Integer.parseInt(productId));
                if (byProductId != null && byProductId.getSendRedBag() == 1) {
                    System.out.println("send red bag: " + productId);
                }
                break;
            case 1:
                // 延时
                MessageProperties messageProperties = new MessageProperties();
                messageProperties.setExpiration(delayServiceTime);
                Message message = new Message(productId.getBytes(), messageProperties);
                rabbitTemplate.send("normalExchange","normalRoutingKey", message);
                System.out.println("延迟处理：" + productId + ":" + delayServiceTime);
                break;
            case 2:
                System.out.println("暂停服务");
                break;
            default:
                throw new UnsupportedOperationException("不支持的服务级别");
        }
    }

    public Order decoratorPay(String orderId, int serviceLevel, float price) {
        Order order = super.pay(orderId);
        try {
            this.updateScoreAndSendRedPaper(orderId, serviceLevel, price);
        } catch (Exception e) {
            //重试机制
        }
        return order;
    }

    @Override
    public void friendPay(String sourceCustomer, String orderId, String targetCustomer, String payResult, String role) {
        Buyer buyer = new Buyer(medicator, orderId,sourceCustomer);
        Payer payer = new Payer(medicator, orderId, sourceCustomer);
        HashMap<String, AbstractCustomer> map = new HashMap<>();
        map.put("buyer",buyer);
        map.put("payer",payer);
        Medicator.customerInstances.put(orderId, map);
        if ("B".equals(role)) {
            buyer.messageTransfer(orderId, targetCustomer, payResult);
        } else if ("A".equals(role)) {
            payer.messageTransfer(orderId, targetCustomer, payResult);
        }
    }
}
